﻿using Decision.BusinessLayer;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using Telerik.Web.UI;
using thaihua.model;

namespace thaihua.service
{
    /// <summary>
    /// Summary description for DropDownStaticFactory
    /// </summary>
    public class DropDownStaticFactory
    {
        public DropDownStaticFactory()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public static void GetSearchBy(ref RadComboBox ddl_Status, bool p)
        {
            ddl_Status.Items.Insert(0, new Telerik.Web.UI.RadComboBoxItem("เลขที่", "CODE_NO"));
            ddl_Status.Items.Insert(1, new Telerik.Web.UI.RadComboBoxItem("เลขวาระ", "TERM_NO"));
            ddl_Status.Items.Insert(2, new Telerik.Web.UI.RadComboBoxItem("ชื่อนิติกรที่ดูแล", "LAW_FNAME"));
            ddl_Status.Items.Insert(3, new Telerik.Web.UI.RadComboBoxItem("มติ", "RESULT_NO"));
            ddl_Status.Items.Insert(4, new Telerik.Web.UI.RadComboBoxItem("ชื่อผู้ร้องและผู้ถูกร้อง", "REQUEST_NAME"));
            ddl_Status.Items.Insert(5, new Telerik.Web.UI.RadComboBoxItem("ข้อกล่าวหา", "ALLEGATION"));
            ddl_Status.Items.Insert(6, new Telerik.Web.UI.RadComboBoxItem("เลขที่คำวินิจฉัย", "DECISION_NO"));
        }

        public static void GetMonth(ref RadComboBox ddl_Status, bool p)
        {
            int currentYear = DateTime.Now.Year;
            for (int i = 2007; i <= currentYear; i++)
            {
                ddl_Status.Items.Insert(i - 2007, new Telerik.Web.UI.RadComboBoxItem(currentYear + (543 - (i - 2007)) + "", currentYear - (i - 2007) + ""));
            }
        }

        public static RadComboBox GetResultType(ref RadComboBox ddl_payTerm, bool isRequire)
        {
            ddl_payTerm.Items.Add(new Telerik.Web.UI.RadComboBoxItem("กรณีไปศาล", "1"));
            ddl_payTerm.Items.Add(new Telerik.Web.UI.RadComboBoxItem("กรณีไม่ไปศาล", "2"));
            if (isRequire)
            {
                ddl_payTerm.Items.Insert(0, new Telerik.Web.UI.RadComboBoxItem(Constant.REQ_FEILD, "-1"));
            }
            return ddl_payTerm;
        }

        //LAWYER_ID
        public static RadComboBox GetCOURT(ref RadComboBox ddl_user, bool isRequire)
        {
            ddl_user.DataTextField = DCS_COURT.DCS_COURTFields.COURT_NAME.ToString();
            ddl_user.DataValueField = DCS_COURT.DCS_COURTFields.COURT_ID.ToString();
            ddl_user.DataSource = new DCS_COURTFactory().GetAll();
            ddl_user.DataBind();
            if (isRequire)
            {
                ddl_user.Items.Insert(0, new Telerik.Web.UI.RadComboBoxItem(Constant.REQ_FEILD, "-1"));
            }
            return ddl_user;
        }

        //LAWYER_ID
        public static RadComboBox GetLawyer(ref RadComboBox ddl_user, bool isRequire)
        {
            List<ECT_USER> listUser = DCSFactory.CreateECTUserFactory().GetAllBy(ECT_USER.ECT_USERFields.USER_GROUP_LVL, 2).Select(u => new ECT_USER 
            {
                USER_ID = u.USER_ID,
                FNAME = u.FNAME + " " + u.LNAME
            }).OrderBy(x => x.FNAME).ToList();

            ddl_user.DataTextField = ECT_USER.ECT_USERFields.FNAME.ToString();
            ddl_user.DataValueField = ECT_USER.ECT_USERFields.USER_ID.ToString();
            ddl_user.DataSource = listUser;
            ddl_user.DataBind();
            if (isRequire)
            {
                ddl_user.Items.Insert(0, new Telerik.Web.UI.RadComboBoxItem(Constant.REQ_FEILD, "-1"));
            }
            return ddl_user;
        }

        //ddl_DECISION_TYPE_ID
        public static RadComboBox GetDecisionType(ref RadComboBox ddl_ApproveBy, bool isRequire)
        {
            DCS_DECISION_TYPEFactory empFac = new DCS_DECISION_TYPEFactory();
            ddl_ApproveBy.DataTextField = DCS_DECISION_TYPE.DCS_DECISION_TYPEFields.DECISION_NAME.ToString();
            ddl_ApproveBy.DataValueField = DCS_DECISION_TYPE.DCS_DECISION_TYPEFields.DCS_DECISION_TYPE_ID.ToString();
            ddl_ApproveBy.DataSource = empFac.GetAll();
            ddl_ApproveBy.DataBind();
            if (isRequire)
            {
                ddl_ApproveBy.Items.Insert(0, new Telerik.Web.UI.RadComboBoxItem(Constant.REQ_FEILD, "-1"));
            }
            return ddl_ApproveBy;
        }


        public static RadComboBox GetCaseType(ref RadComboBox ddl_Suppliers, bool isRequire)
        {
            DCS_CASE_TYPEFactory fac = new DCS_CASE_TYPEFactory();
            ddl_Suppliers.DataSource = fac.GetAll().OrderBy(x => x.CASE_TYPE_NAME).ToList();
            ddl_Suppliers.DataValueField = DCS_CASE_TYPE.DCS_CASE_TYPEFields.CASE_TYPE_ID.ToString();
            ddl_Suppliers.DataTextField = DCS_CASE_TYPE.DCS_CASE_TYPEFields.CASE_TYPE_NAME.ToString();
            ddl_Suppliers.DataBind();
            if (isRequire)
            {
                ddl_Suppliers.Items.Insert(0, new Telerik.Web.UI.RadComboBoxItem(Constant.REQ_FEILD, "-1"));
            }
            return ddl_Suppliers;
        }

        public static RadComboBox GetVoteType(ref RadComboBox ddl_Suppliers, bool isRequire)
        {
            DCS_VOTE_TYPEFactory fac = new DCS_VOTE_TYPEFactory();
            ddl_Suppliers.DataSource = fac.GetAll().OrderBy(x => x.VOTE_NAME).ToList();
            ddl_Suppliers.DataValueField = DCS_VOTE_TYPE.DCS_VOTE_TYPEFields.VOTE_TYPE_ID.ToString();
            ddl_Suppliers.DataTextField = DCS_VOTE_TYPE.DCS_VOTE_TYPEFields.VOTE_NAME.ToString();
            ddl_Suppliers.DataBind();
            if (isRequire)
            {
                ddl_Suppliers.Items.Insert(0, new Telerik.Web.UI.RadComboBoxItem(Constant.REQ_FEILD, "-1"));
            }
            return ddl_Suppliers;
        }

        public static RadComboBox GetProvince(ref RadComboBox ddl_Suppliers, bool isRequire)
        {
            ddl_Suppliers.DataSource = new DCS_PROVINCEFactory().GetAll().OrderBy(x => x.PROVINCE_NAME).ToList();
            ddl_Suppliers.DataValueField = DCS_PROVINCE.DCS_PROVINCEFields.PROVINCE_ID.ToString();
            ddl_Suppliers.DataTextField = DCS_PROVINCE.DCS_PROVINCEFields.PROVINCE_NAME.ToString();
            ddl_Suppliers.DataBind();
            if (isRequire)
            {
                ddl_Suppliers.Items.Insert(0, new Telerik.Web.UI.RadComboBoxItem(Constant.REQ_FEILD, "-1"));
            }
            return ddl_Suppliers;
        }

        public static RadComboBox GetAmphur(ref RadComboBox ddl_Suppliers,string provinceID, bool isRequire)
        {
            ddl_Suppliers.DataSource = new DCS_AMPHURFactory().GetAllBy(DCS_AMPHUR.DCS_AMPHURFields.PROVINCE_ID, provinceID).OrderBy(x => x.AMPHUR_NAME).ToList();
            ddl_Suppliers.DataValueField = DCS_AMPHUR.DCS_AMPHURFields.AMPHUR_ID.ToString();
            ddl_Suppliers.DataTextField = DCS_AMPHUR.DCS_AMPHURFields.AMPHUR_NAME.ToString();
            ddl_Suppliers.DataBind();
            if (isRequire)
            {
                ddl_Suppliers.Items.Insert(0, new Telerik.Web.UI.RadComboBoxItem(Constant.REQ_FEILD, "-1"));
            }
            return ddl_Suppliers;
        }

        public static RadComboBox GetTumbol(ref RadComboBox ddl_Suppliers,string amphurID, bool isRequire)
        {
            ddl_Suppliers.DataSource = new DCS_DISTRICTFactory().GetAllBy(DCS_DISTRICT.DCS_DISTRICTFields.AMPHUR_ID, amphurID).OrderBy(x => x.DISTRICT_NAME).ToList();
            ddl_Suppliers.DataValueField = DCS_DISTRICT.DCS_DISTRICTFields.DISTRICT_ID.ToString();
            ddl_Suppliers.DataTextField = DCS_DISTRICT.DCS_DISTRICTFields.DISTRICT_NAME.ToString();
            ddl_Suppliers.DataBind();
            if (isRequire)
            {
                ddl_Suppliers.Items.Insert(0, new Telerik.Web.UI.RadComboBoxItem(Constant.REQ_FEILD, "-1"));
            }
            return ddl_Suppliers;
        }

        public static RadComboBox GetMemberType(ref RadComboBox ddl_Suppliers, bool isRequire)
        {
            ddl_Suppliers.DataSource = new DCS_MEMBER_TYPEFactory().GetAll().OrderBy(x => x.MEMBER_NAME).ToList();
            ddl_Suppliers.DataValueField = DCS_MEMBER_TYPE.DCS_MEMBER_TYPEFields.MEMBER_TYPE_ID.ToString();
            ddl_Suppliers.DataTextField = DCS_MEMBER_TYPE.DCS_MEMBER_TYPEFields.MEMBER_NAME.ToString();
            ddl_Suppliers.DataBind();
            if (isRequire)
            {
                ddl_Suppliers.Items.Insert(0, new Telerik.Web.UI.RadComboBoxItem(Constant.REQ_FEILD, "-1"));
            }
            return ddl_Suppliers;
        }

        //DDL_COURT_CONSTITU_TYPE
        public static RadComboBox GetCourtConstituType(ref RadComboBox ddl_Suppliers, bool isRequire)
        {
            ddl_Suppliers.DataSource = new DCS_COURT_CONSTITU_TYPEFactory().GetAll().OrderBy(x => x.COURT_NAME).ToList();
            ddl_Suppliers.DataValueField = DCS_COURT_CONSTITU_TYPE.DCS_COURT_CONSTITU_TYPEFields.COURT_CONSTITU_TYPE_ID.ToString();
            ddl_Suppliers.DataTextField = DCS_COURT_CONSTITU_TYPE.DCS_COURT_CONSTITU_TYPEFields.COURT_NAME.ToString();
            ddl_Suppliers.DataBind();
            if (isRequire)
            {
                ddl_Suppliers.Items.Insert(0, new Telerik.Web.UI.RadComboBoxItem(Constant.REQ_FEILD, "-1"));
            }
            return ddl_Suppliers;
        }

        //DDL_COURT_B_SUPREME_TYPE_ID

        public static RadComboBox GetCourtASupremeType(ref RadComboBox ddl_Suppliers, bool isRequire)
        {
            ddl_Suppliers.DataSource = new DCS_COURT_A_SUPREME_TYPEFactory().GetAll().OrderBy(x => x.COURT_A_SUPREME_TYPE_ID).ToList();
            ddl_Suppliers.DataValueField = DCS_COURT_A_SUPREME_TYPE.DCS_COURT_A_SUPREME_TYPEFields.COURT_A_SUPREME_TYPE_ID.ToString();
            ddl_Suppliers.DataTextField = DCS_COURT_A_SUPREME_TYPE.DCS_COURT_A_SUPREME_TYPEFields.COURT_NAME.ToString();
            ddl_Suppliers.DataBind();
            if (isRequire)
            {
                ddl_Suppliers.Items.Insert(0, new Telerik.Web.UI.RadComboBoxItem(Constant.REQ_FEILD, "-1"));
            }
            return ddl_Suppliers;
        }

        public static RadComboBox GetUserGroupLvl(ref RadComboBox ddl_Suppliers, bool isRequire)
        {
            ddl_Suppliers.DataSource = new ECT_GROUP_LVLFactory().GetAll().OrderBy(x => x.GROUP_LVL_NAME).ToList();
            ddl_Suppliers.DataValueField = ECT_GROUP_LVL.ECT_GROUP_LVLFields.GROUP_LVL_ID.ToString();
            ddl_Suppliers.DataTextField = ECT_GROUP_LVL.ECT_GROUP_LVLFields.GROUP_LVL_NAME.ToString();
            ddl_Suppliers.DataBind();
            if (isRequire)
            {
                ddl_Suppliers.Items.Insert(0, new Telerik.Web.UI.RadComboBoxItem(Constant.SELECT_ALL, "-1"));
            }
            return ddl_Suppliers;
        }

        public static RadComboBox GetINJUNCTION_COURT(ref RadComboBox ddl_Suppliers, bool isRequire)
        {
            ddl_Suppliers.DataSource = new DCS_INJUNCTION_COURTFactory().GetAll().OrderBy(x => x.INJUNCTION_COURT_ID).ToList();
            ddl_Suppliers.DataValueField = DCS_INJUNCTION_COURT.DCS_INJUNCTION_COURTFields.INJUNCTION_COURT_ID.ToString();
            ddl_Suppliers.DataTextField = DCS_INJUNCTION_COURT.DCS_INJUNCTION_COURTFields.INJUNCTION_COURT_NAME.ToString();
            ddl_Suppliers.DataBind();
            if (isRequire)
            {
                ddl_Suppliers.Items.Insert(0, new Telerik.Web.UI.RadComboBoxItem(Constant.REQ_FEILD, "-1"));
            }
            return ddl_Suppliers;
        }

        static readonly string SQL_STEP = "SELECT *, (SELECT STATUS FROM DCS_STEP_01 WHERE DCS_MASTER_ID = @DCS_MASTER_ID ) AS STATUS FROM DCS_STEP WHERE DCS_STEP.STEP_ID = 1 " +
                "UNION ALL " +
                "SELECT *, (SELECT STATUS FROM DCS_STEP_02 WHERE DCS_MASTER_ID = @DCS_MASTER_ID ) AS STATUS FROM DCS_STEP WHERE DCS_STEP.STEP_ID = 2 " +
                "UNION ALL " +
                "SELECT *, (SELECT STATUS FROM DCS_STEP_03 WHERE DCS_MASTER_ID = @DCS_MASTER_ID ) AS STATUS FROM DCS_STEP WHERE DCS_STEP.STEP_ID = 3 " +
                "UNION ALL " +
                "SELECT *, (SELECT STATUS FROM DCS_STEP_04 WHERE DCS_MASTER_ID = @DCS_MASTER_ID ) AS STATUS FROM DCS_STEP WHERE DCS_STEP.STEP_ID = 4 " +
                "UNION ALL " +
                "SELECT *, (SELECT STATUS FROM DCS_STEP_05 WHERE DCS_MASTER_ID = @DCS_MASTER_ID ) AS STATUS FROM DCS_STEP WHERE DCS_STEP.STEP_ID = 5 " +
                "UNION ALL " +
                "SELECT *, (SELECT STATUS FROM DCS_STEP_06 WHERE DCS_MASTER_ID = @DCS_MASTER_ID ) AS STATUS FROM DCS_STEP WHERE DCS_STEP.STEP_ID = 6 " +
                "UNION ALL " +
                "SELECT *, (SELECT STATUS FROM DCS_STEP_07 WHERE DCS_MASTER_ID = @DCS_MASTER_ID ) AS STATUS FROM DCS_STEP WHERE DCS_STEP.STEP_ID = 7 " +
                "UNION ALL " +
                "SELECT *, (SELECT STATUS FROM DCS_STEP_08 WHERE DCS_MASTER_ID = @DCS_MASTER_ID ) AS STATUS FROM DCS_STEP WHERE DCS_STEP.STEP_ID = 8 " +
                "UNION ALL " +
                "SELECT *, (SELECT STATUS FROM DCS_STEP_09 WHERE DCS_MASTER_ID = @DCS_MASTER_ID ) AS STATUS FROM DCS_STEP WHERE DCS_STEP.STEP_ID = 9 " +
                "UNION ALL " +
                "SELECT *, (SELECT STATUS FROM DCS_STEP_10 WHERE DCS_MASTER_ID = @DCS_MASTER_ID ) AS STATUS FROM DCS_STEP WHERE DCS_STEP.STEP_ID = 10 " +
                "UNION ALL " +
                "SELECT *, (SELECT STATUS FROM DCS_STEP_11 WHERE DCS_MASTER_ID = @DCS_MASTER_ID ) AS STATUS FROM DCS_STEP WHERE DCS_STEP.STEP_ID = 11 " +
                "UNION ALL " +
                "SELECT *, (SELECT STATUS FROM DCS_STEP_12 WHERE DCS_MASTER_ID = @DCS_MASTER_ID ) AS STATUS FROM DCS_STEP WHERE DCS_STEP.STEP_ID = 12 " +
                "UNION ALL " +
                "SELECT *, (SELECT STATUS FROM DCS_STEP_13 WHERE DCS_MASTER_ID = @DCS_MASTER_ID ) AS STATUS FROM DCS_STEP WHERE DCS_STEP.STEP_ID = 13 " +
                "UNION ALL " +
                "SELECT *, (SELECT STATUS FROM DCS_STEP_14 WHERE DCS_MASTER_ID = @DCS_MASTER_ID ) AS STATUS FROM DCS_STEP WHERE DCS_STEP.STEP_ID = 14 " +
                "UNION ALL " +
                "SELECT *, (SELECT STATUS FROM DCS_STEP_15 WHERE DCS_MASTER_ID = @DCS_MASTER_ID ) AS STATUS FROM DCS_STEP WHERE DCS_STEP.STEP_ID = 15";

        public static DataTable GetViewStepStatus(string masterID)
        {
            string sql =SQL_STEP.Replace("@DCS_MASTER_ID", masterID);

            return Util.GetDataTable(sql);
        }
        public static DataTable GetViewStepStatus(string masterID,string inStep)
        {
            string sql = SQL_STEP.Replace("@DCS_MASTER_ID", masterID);
            sql = "SELECT * FROM  ( " + sql + ") X WHERE X.STEP_ID in (" + inStep + ")";
            return Util.GetDataTable(sql);
        }

        public static RadComboBox GetCourtAppType(ref RadComboBox DDL_COURT_APPEALS_TYP_ID, bool isRequire)
        {
            DDL_COURT_APPEALS_TYP_ID.DataSource = new DCS_COURT_APPEALSFactory().GetAll().OrderBy(x => x.COURT_APPEALS_NAME).ToList();
            DDL_COURT_APPEALS_TYP_ID.DataValueField = DCS_COURT_APPEALS.DCS_COURT_APPEALSFields.COURT_APPEALS_ID.ToString();
            DDL_COURT_APPEALS_TYP_ID.DataTextField = DCS_COURT_APPEALS.DCS_COURT_APPEALSFields.COURT_APPEALS_NAME.ToString();
            DDL_COURT_APPEALS_TYP_ID.DataBind();
            if (isRequire)
            {
                DDL_COURT_APPEALS_TYP_ID.Items.Insert(0, new Telerik.Web.UI.RadComboBoxItem(Constant.SELECT_ALL, "-1"));
            }
            return DDL_COURT_APPEALS_TYP_ID;
        }
    }
}